package com.metamech.wocky;

import com.metamech.jabber.Authenticator;
import com.metamech.jabber.Session;
import com.metamech.jabber.xml.Packet;
import com.metamech.log.Log;
/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:
 * @author
 * @version 1.0
 */
class SimpleAuthMessageClient2 {

  public static void main(String[] args){
    System.out.print("Initializing authenticator...");
    Authenticator.randomToken();
    System.out.println(" initialized.");    
    new SimpleAuthMessageClient2();
  }

  	public SimpleAuthMessageClient2(){
  		String server =  System.getProperty("jab.server.name",   "mobbuzz.com");
  		String address = System.getProperty("jab.server.address","127.0.0.1");
  		//String address = System.getProperty("jab.server.address","http://mobbuzzworld.s155.eatj.com");
  		String port =    System.getProperty("jab.server.port",   "5222");
  		String auth =	 System.getProperty("jab.server.auth",   "plain");
	
  		//BuffyTestThread buffyTT = new BuffyTestThread();
  		//JabberModel buffyModel = new JabberModel(buffyTT);
  		AngelTestThread angelTT = new AngelTestThread();
  		JabberModel angelModel = new JabberModel(angelTT);
	
  		/*buffyModel.setServerName(server);
  		buffyModel.setServerAddress(address);
  		buffyModel.setPort(port);
  		buffyModel.setAuthMode(auth);
  		
  		buffyModel.setUser("buffy");
  		buffyModel.setResource("home");
  		buffyModel.setPassword("buffPass");*/
  		
  		angelModel.setServerName(server);
  		angelModel.setServerAddress(address);
  		angelModel.setPort(port);
  		angelModel.setAuthMode(auth);
  		
  		angelModel.setUser("angel");
  		angelModel.setResource("home");
  		angelModel.setPassword("angelPass");
  		
  		//buffyTT.setModel(buffyModel);
  		//buffyTT.start();*/
  		angelTT.setModel(angelModel);
  		angelTT.start();
  	}
  	
  	public class BuffyTestThread extends TestThread {
  		
  		public void run(){
  			try {    
  				Log.info("Buffy: Connecting");
  				model.connect();
  				//Log.info("Buffy: registering");
  				//model.register();
  				Log.info("Buffy: Authenticating");
  				model.authenticate();
  				Log.info("Buffy: Waiting for authentication");
  				do {
  					notifyHandlers(packetQueue.pull());
  				} while (model.getSessionStatus() != Session.AUTHENTICATED);
  				Log.info("Buffy: Sending message");
  				model.sendMessage("buffy@" + model.getServerName(),
                          	"Want to patrol?",
                          	"thread_id",
                          	"normal",
                          	"msg_id_buffy",
                          	"<rss type='reg'><title>detik.com</title><url>http://rss.detik.com/index.php/detikcom</url></rss>");
  				Log.info("Buffy: waiting for message from angel");
  				waitFor("message",null);
  				Log.info("Buffy: disconnecting");
  				model.disconnect();
  			} catch (Exception ex){
  				ex.printStackTrace();
  			}
  		} // run()
  	}
  	public class AngelTestThread extends TestThread {
  		
  		public void run(){
  			try {    
  				Log.info("Angel: connecting");
  				model.connect();
  				//Log.info("Angel: registering");
  				//model.register();
  				Log.info("Angel: authenticating");
  				model.authenticate();
  				Log.info("Angel: waiting for authentication");
  				do {
  					notifyHandlers(packetQueue.pull());
  				} while (model.getSessionStatus() != Session.AUTHENTICATED);
  				Log.info("Angel: waiting for message from buffy");
  				for (Packet packet = waitFor("message",null);
  				!packet.getFrom().startsWith("buffy");
  				packet = waitFor("message",null)){
  					Log.info("Angel: message from " + packet.getFrom());
  				}
  				Log.info("Angel: sending message to buffy");
  				model.sendMessage("buffy@" + model.getServerName(),
                          	"Re: Want to patrol?",
                          	"thread_id",
                          	"normal",
                          	"msg_id_angel",
                          	"Sure, I'd love to go.");
  				Log.info("Angel: disconnecting");
  				model.disconnect();
  			} catch (Exception ex){
  				ex.printStackTrace();
  			}
  		} // run()
  	}
}
